package com.cily.utils.t_webrtc.singlr.phone;

import android.os.Bundle;
import android.view.View;

import com.cily.utils.app.event.Event;
import com.cily.utils.t_webrtc.BaseAc3;
import com.cily.utils.t_webrtc.Conf;
import com.cily.utils.t_webrtc.R;
import com.cily.utils.t_webrtc.pad.VideoAc;
import com.cily.utils.t_webrtc.singlr.Beans.MessageBean;

public class AcceptActivity extends BaseAc3 {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_accept);

        final String ice = getIntent().getStringExtra(Conf.INTENT_ICECANDIDATE);
        final String callUser = getIntent().getStringExtra(Conf.INTENT_CALL_USER);

        findBtn(R.id.button_accept).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                toVideo(callUser, ice);
            }
        });
        findBtn(R.id.button_reject).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });

        play();
        startTimer();
    }

    @Override
    public void doEvent(Event e) {
        final MessageBean messageBean = (MessageBean)e.obj;
        if(messageBean != null) {
            int code = messageBean.getCode();
            if(code == Conf.ACTION_CALL_VIDEO || code ==Conf.ACTION_CALL_VOICE) {
                //呼机请求 跳转到 accept ;
//                Intent showAccept=new Intent(ProfileActivity.this,AcceptFragment.class);
//                Bundle bundle = new Bundle();
//                bundle.putString("reciver",messageBean.getSender().getConnectionId() );
//                showAccept.putExtras(bundle);
//                startActivity(showAccept);
                //startTimer();
                type_call = code;
                // play();
//                toVideo(messageBean.getReciverId());
            }else if (code == Conf.ACTION_BUSY){
                next("对方忙，请稍后呼叫");
            }else if (code == Conf.ACTION_REFUSE){
                next("拒绝接听");
            }else if (code == Conf.ACTION_USER_NOT_EXIST){
                next("该房间为空号");
            }else if (code == Conf.ACTION_HUNG_UP){
                next("聊天已结束");
            }else if (code == Conf.ACTION_ERROR){
                next("未知错误，聊天已结束");
            }else if (code == Conf.ACTION_AGREE){
                //接听
//                toVideo(messageBean.getReciverId());

            }else if (code == Conf.ACTION_HOLD){
                cancelTimer();
            }
        }

        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                debugToast(messageBean.toString());
            }
        });
    }

    private void next(final String str){
        stop();
        cancelTimer();

        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                showToast(str);
            }
        });
    }

    private int type_call = Conf.ACTION_CALL_VIDEO;
    private void toVideo(String reciever, String ice){
        cancelTimer();
        stop();

        Bundle b = new Bundle();
        b.putString(Conf.EXTRA_ROOMID, reciever);
        b.putInt(Conf.INTENT_CALL_TYPE, type_call);
        b.putString(Conf.INTENT_ICECANDIDATE, ice);
        toAc(VideoAc.class, b);
        finish();
    }
}